package org.west.sky.scripture.exports.biz;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.west.sky.scripture.exports.core.ExcelFillCellRowMergeStrategy;
import org.west.sky.scripture.exports.core.ExportUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: chz
 * date: 2025/9/1
 * description:
 */
@RequestMapping("score")
@RestController
public class ExportScoreController {

    @GetMapping("export")
    public void export(HttpServletResponse response) {
        ExcelFillCellRowMergeStrategy mergeStrategy = new ExcelFillCellRowMergeStrategy(2, new int[]{0}, true);
        try {
            ExportUtils.fillExport(response, "分数统计数据导出", "分数统计.xlsx", mergeStrategy, initData());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 模拟数据
     *
     * @return
     */
    private Map<String, Object> initData() {
        //数据和模板保持一致
        Map<String, Object> map = new HashMap<>();

        HeadData headData = new HeadData();
        headData.setYear("2023");
        headData.setClassName("八年级(1)班");
        map.put("head", headData);

        List<BodyData> bodyDataList = new ArrayList<>();
        bodyDataList.add(new BodyData("语文", "小王", 80));
        bodyDataList.add(new BodyData("语文", "小李", 90));
        bodyDataList.add(new BodyData("语文", "小吴", 70));
        bodyDataList.add(new BodyData("语文", "小赵", 60));

        bodyDataList.add(new BodyData("数学", "小王", 80));
        bodyDataList.add(new BodyData("数学", "小李", 90));
        bodyDataList.add(new BodyData("数学", "小吴", 70));
        bodyDataList.add(new BodyData("数学", "小赵", 60));

        bodyDataList.add(new BodyData("英语", "小王", 80));
        bodyDataList.add(new BodyData("英语", "小李", 90));
        bodyDataList.add(new BodyData("英语", "小吴", 70));
        bodyDataList.add(new BodyData("英语", "小赵", 60));
        map.put("data", bodyDataList);

        FootData footData = new FootData();
        footData.setTotalScore(500);
        map.put("foot", footData);
        return map;
    }

    @Getter
    @Setter
    public static class HeadData {

        /**
         * 年度
         */
        private String year;
        /**
         * 班级名称
         */
        private String className;
    }

    @Getter
    @Setter
    @AllArgsConstructor
    public static class BodyData {

        /**
         * 学科
         */
        private String subject;
        /**
         * 学生姓名
         */
        private String name;
        /**
         * 分数
         */
        private double score;
    }

    @Getter
    @Setter
    public static class FootData {

        /**
         * 总分
         */
        private double totalScore;
    }
}
